<template>
  <div class="class-history">

    <div class="main">
      <div class="filter-section">
        <div class="filter-tabs">
          <div
            class="filter-tab"
            :class="{ active: currentFilter === 'all' }"
            @click="changeFilter('all')"
          >
            全部
          </div>
          <div
            class="filter-tab"
            :class="{ active: currentFilter === 'completed' }"
            @click="changeFilter('completed')"
          >
            已完成
          </div>
          <div
            class="filter-tab"
            :class="{ active: currentFilter === 'pending' }"
            @click="changeFilter('pending')"
          >
            待完成
          </div>
        </div>
      </div>

      <div class="history-list" v-if="historyList.length > 0">
        <div
          class="history-item"
          v-for="item in filteredList"
          :key="item.id"
          :class="{ completed: item.status === 'completed' }"
        >
          <div class="item-header">
            <div class="class-name">{{ item.className }}</div>
            <div class="status" :class="item.status">
              {{ getStatusText(item.status) }}
            </div>
          </div>
          <div class="item-content">
            <div class="info-row">
              <span class="label">上课时间：</span>
              <span class="value">{{ item.classTime }}</span>
            </div>
            <div class="info-row">
              <span class="label">授课老师：</span>
              <span class="value">{{ item.teacherName }}</span>
            </div>
            <div class="info-row">
              <span class="label">上课地点：</span>
              <span class="value">{{ item.location }}</span>
            </div>
            <div class="info-row" v-if="item.notes">
              <span class="label">课程备注：</span>
              <span class="value">{{ item.notes }}</span>
            </div>
          </div>
          <div class="item-footer">
            <div class="date">{{ item.date }}</div>
            <div class="duration">{{ item.duration }}分钟</div>
          </div>
        </div>
      </div>

      <div class="empty-state" v-else>
        <div class="empty-icon">📚</div>
        <div class="empty-text">暂无课时记录</div>
        <div class="empty-desc">您还没有相关的课时历史记录</div>
      </div>
    </div>

    <div class="loading" v-if="loading">
      <div class="loading-spinner"></div>
      <div class="loading-text">加载中...</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      buyId: '',
      currentFilter: 'all',
      loading: false,
      historyList: [
        {
          id: 1,
          className: '数学基础课程',
          classTime: '2024-10-18 14:00-15:30',
          teacherName: '张老师',
          location: 'A栋201教室',
          status: 'completed',
          date: '2024-10-18',
          duration: 90,
          notes: '重点讲解函数与导数'
        },
        {
          id: 2,
          className: '英语口语训练',
          classTime: '2024-10-20 10:00-11:00',
          teacherName: '李老师',
          location: 'B栋105教室',
          status: 'completed',
          date: '2024-10-20',
          duration: 60,
          notes: '情景对话练习'
        },
        {
          id: 3,
          className: '物理实验课',
          classTime: '2024-10-25 15:00-16:30',
          teacherName: '王老师',
          location: '实验楼301',
          status: 'pending',
          date: '2024-10-25',
          duration: 90,
          notes: '力学实验操作'
        },
        {
          id: 4,
          className: '化学专题讲解',
          classTime: '2024-10-22 09:00-10:30',
          teacherName: '赵老师',
          location: 'C栋208教室',
          status: 'completed',
          date: '2024-10-22',
          duration: 90,
          notes: '有机化学反应机理'
        }
      ]
    }
  },
  computed: {
    filteredList() {
      if (this.currentFilter === 'all') {
        return this.historyList
      } else if (this.currentFilter === 'completed') {
        return this.historyList.filter(item => item.status === 'completed')
      } else if (this.currentFilter === 'pending') {
        return this.historyList.filter(item => item.status === 'pending')
      }
      return this.historyList
    }
  },
  created() {
    this.buyId = this.$route.params.buyId
    this.loadHistoryData()
  },
  methods: {
    goBack() {
      this.$router.go(-1)
    },
    changeFilter(filter) {
      this.currentFilter = filter
    },
    getStatusText(status) {
      const statusMap = {
        completed: '已完成',
        pending: '待完成',
        cancelled: '已取消'
      }
      return statusMap[status] || '未知状态'
    },
    async loadHistoryData() {
      this.loading = true
      try {
        // 这里应该调用API获取真实的课时历史数据
        // const res = await this.$http.getClassHistory({ buyId: this.buyId })
        // this.historyList = res.data.result || []

        // 模拟API调用延迟
        await new Promise(resolve => setTimeout(resolve, 1000))

        // 在实际项目中，这里应该是从API获取的数据
        // this.historyList = res.data.result || []

      } catch (error) {
        console.error('加载课时历史失败:', error)
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.class-history {
  width: 100%;
  min-height: 100vh;
  background-color: #f5f5f5;
  display: flex;
  flex-direction: column;

  .header {
    background: #fff;
    padding: 15px 20px;
    border-bottom: 1px solid #eee;
    display: flex;
    align-items: center;
    position: relative;

    .back-btn {
      position: absolute;
      left: 20px;
      top: 50%;
      transform: translateY(-50%);
      width: 30px;
      height: 30px;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;

      .back-icon {
        font-size: 18px;
        color: #333;
      }
    }

    .header-title {
      font-size: 18px;
      font-weight: 600;
      color: #333;
      margin: 0;
      text-align: center;
      flex: 1;
    }
  }

  .main {
    flex: 1;
    padding: 15px;

    .filter-section {
      background: #fff;
      border-radius: 8px;
      margin-bottom: 15px;
      padding: 15px;

      .filter-tabs {
        display: flex;
        background: #f5f5f5;
        border-radius: 6px;
        padding: 4px;

        .filter-tab {
          flex: 1;
          text-align: center;
          padding: 8px 0;
          border-radius: 4px;
          font-size: 14px;
          color: #666;
          cursor: pointer;
          transition: all 0.3s ease;

          &.active {
            background: #667eea;
            color: #fff;
            font-weight: 500;
          }
        }
      }
    }

    .history-list {
      .history-item {
        background: #fff;
        border-radius: 8px;
        margin-bottom: 12px;
        padding: 16px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);

        &.completed {
          border-left: 4px solid #52c41a;
        }

        &:not(.completed) {
          border-left: 4px solid #faad14;
        }

        .item-header {
          display: flex;
          justify-content: space-between;
          align-items: flex-start;
          margin-bottom: 12px;

          .class-name {
            font-size: 16px;
            font-weight: 600;
            color: #333;
            flex: 1;
            margin-right: 10px;
          }

          .status {
            font-size: 12px;
            padding: 4px 8px;
            border-radius: 12px;
            font-weight: 500;

            &.completed {
              background: #f6ffed;
              color: #52c41a;
              border: 1px solid #b7eb8f;
            }

            &.pending {
              background: #fff7e6;
              color: #fa8c16;
              border: 1px solid #ffd591;
            }
          }
        }

        .item-content {
          .info-row {
            display: flex;
            margin-bottom: 8px;
            font-size: 14px;

            .label {
              color: #666;
              width: 80px;
              flex-shrink: 0;
            }

            .value {
              color: #333;
              flex: 1;
              word-break: break-all;
            }

            &:last-child {
              margin-bottom: 0;
            }
          }
        }

        .item-footer {
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin-top: 12px;
          padding-top: 12px;
          border-top: 1px solid #f0f0f0;

          .date {
            font-size: 12px;
            color: #999;
          }

          .duration {
            font-size: 12px;
            color: #666;
            font-weight: 500;
          }
        }
      }
    }

    .empty-state {
      text-align: center;
      padding: 60px 20px;

      .empty-icon {
        font-size: 48px;
        margin-bottom: 16px;
        opacity: 0.6;
      }

      .empty-text {
        font-size: 16px;
        color: #666;
        margin-bottom: 8px;
      }

      .empty-desc {
        font-size: 14px;
        color: #999;
      }
    }
  }

  .loading {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.9);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 1000;

    .loading-spinner {
      width: 40px;
      height: 40px;
      border: 3px solid #f3f3f3;
      border-top: 3px solid #667eea;
      border-radius: 50%;
      animation: spin 1s linear infinite;
      margin-bottom: 16px;
    }

    .loading-text {
      font-size: 14px;
      color: #666;
    }
  }
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 移动端适配 */
@media (max-width: 768px) {
  .class-history {
    .header {
      padding: 12px 15px;

      .header-title {
        font-size: 16px;
      }

      .back-btn {
        left: 15px;
        width: 28px;
        height: 28px;

        .back-icon {
          font-size: 16px;
        }
      }
    }

    .main {
      padding: 10px;

      .filter-section {
        padding: 12px;
        margin-bottom: 12px;

        .filter-tabs {
          .filter-tab {
            font-size: 13px;
            padding: 6px 0;
          }
        }
      }

      .history-list {
        .history-item {
          padding: 14px;
          margin-bottom: 10px;

          .item-header {
            .class-name {
              font-size: 15px;
            }
          }

          .item-content {
            .info-row {
              font-size: 13px;

              .label {
                width: 70px;
              }
            }
          }
        }
      }

      .empty-state {
        padding: 40px 15px;

        .empty-icon {
          font-size: 40px;
        }

        .empty-text {
          font-size: 15px;
        }

        .empty-desc {
          font-size: 13px;
        }
      }
    }
  }
}

/* 小屏幕手机适配 */
@media (max-width: 375px) {
  .class-history {
    .main {
      .history-list {
        .history-item {
          .item-content {
            .info-row {
              .label {
                width: 65px;
              }
            }
          }
        }
      }
    }
  }
}
</style>
